package com.wss.module.wan.ui.search;

import cn.bingoogolapple.refreshlayout.BgarefreshLayout;
import com.wss.common.base.BaseRefreshListAbility;
import com.wss.common.base.adapter.listener.OnListItemClickListener;
import com.wss.common.manage.AbilityToAbility;
import com.wss.common.utils.ToastUtils;
import com.wss.common.widget.ObserverButton;
import com.wss.module.wan.ResourceTable;
import com.wss.module.wan.bean.Article;
import com.wss.module.wan.ui.main.adapter.ArticleAdapter;
import com.wss.module.wan.ui.search.mvp.SearchPresenter;
import com.wss.module.wan.ui.search.mvp.contract.SearchContract;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import java.util.ArrayList;
import java.util.List;

public class SearchAbility extends BaseRefreshListAbility<SearchPresenter>
        implements SearchContract.View {

    private TextField editText;
    private List<Article> data = new ArrayList<>();
    private int page = 0;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
    }

    @Override
    protected void initView() {
        super.initView();
        Component actionBarView = LayoutScatter.getInstance(this)
                .parse(ResourceTable.Layout_wan_action_bar_of_search, null, false);
        editText = (TextField) actionBarView.findComponentById(ResourceTable.Id_edt_search);
        ObserverButton oButton = (ObserverButton) actionBarView.findComponentById(ResourceTable.Id_obt_search);
        oButton.observer(editText);
        getTitleBar().setActionBarView(actionBarView);
        editText.setEditorActionListener(new Text.EditorActionListener() {
            @Override
            public boolean onTextEditorAction(int i) {
                resetPage();
                return true;
            }
        });

        findComponentById(ResourceTable.Id_ll_back).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                terminateAbility();
            }
        });

        findComponentById(ResourceTable.Id_obt_search).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                resetPage();
            }
        });
    }

    private void resetPage() {
        page = 0;
        data.clear();
        getPresenter().search();
    }

    @Override
    protected SearchPresenter createPresenter() {
        return new SearchPresenter();
    }

    @Override
    protected BgarefreshLayout.BgarefreshLayoutDelegate createRefreshListener() {
        return new BgarefreshLayout.BgarefreshLayoutDelegate() {
            @Override
            public void onBgarefreshLayoutBeginRefreshing(BgarefreshLayout bgarefreshLayout) {
                resetPage();
            }

            @Override
            public boolean onBgarefreshLayoutBeginLoadingMore(BgarefreshLayout bgarefreshLayout) {
                getPresenter().search();
                return false;
            }
        };
    }

    @Override
    protected BaseItemProvider createAdapter() {
        return new ArticleAdapter(context, data, new OnListItemClickListener<Article>() {
            @Override
            public void onItemClick(Article data1, int position) {
                AbilityToAbility.toWebView(context, data1.getLink());
            }
        });
    }

    @Override
    public String getWord() {
        return editText.getText().toString().trim();
    }

    @Override
    public int getPage() {
        return page;
    }

    @Override
    public void refreshSearchData(List<Article> searchs) {
        int position = this.data.size();
        this.data.addAll(searchs);
        getAdapter().notifyDataChanged();
        page++;
        stopRefresh();
    }
    @Override
    public void onError(Object tag, String errorMsg) {
        super.onError(tag, errorMsg);
        if (page == 0) {
            showErrorView(errorMsg);
        } else {
            ToastUtils.show(errorMsg);
        }
    }

    @Override
    public void onEmpty(Object tag) {
        super.onEmpty(tag);
        if (page == 0) {
            showEmptyView("木有搜到你想要的");
        } else {
            ToastUtils.show("没有更多啦~");
        }
    }

    @Override
    protected void onRefreshRetry() {
        super.onRefreshRetry();
        getPresenter().search();
    }
}
